#ifndef SROS_DEVICE_ENCODER_DEVICE_HPP
#define SROS_DEVICE_ENCODER_DEVICE_HPP

#include "config/encoder_config.hpp"

#include "device_impl.hpp"
#include "devices/detail/driver_impl.hpp"

namespace sros::device
{
    class McuDevice;
    class EncoderDevice final : public device_impl<cfg::EncoderCfg>
    {
    public:
        EncoderDevice(std::shared_ptr<McuDevice> mcu_device);
        ~EncoderDevice() override;

        struct Data
        {
            uint8_t dir = 0;    // 0水平 1垂直
            int32_t pos = 0;    // mm
            int32_t pulse = 0;  // 脉冲计数
        };

        RetBool init() override;

        void run() override;

        void close() override;

        void parse(const std::map<uint32_t, int32_t> &data);

        inline std::map<uint32_t, Data> data() { std::lock_guard<std::mutex> lck(mtx_); return data_; }
        
    protected:
        // 配置加载
        RetBool load_config() override;

    private:
        std::mutex mtx_;
        cfg::EncoderCfg cfg_;

        std::shared_ptr<McuDevice> mcu_;
        std::map<uint32_t, Data> data_;
    };
}

#endif